<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="utf-8">
    <title>sendmail.js - Documentation</title>
    
    
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
    <script src="scripts/nav.js" defer></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav >
    
    <input type="text" id="nav-search" placeholder="Search" />
    
    <h2><a href="index.html">Home</a></h2><h2><a href="https://github.com/axolo/egg-sendmail" target="_blank" class="menu-item" id="website_link" >GitHub</a></h2><h3>Classes</h3><ul><li><a href="Sendmail.html">Sendmail</a><ul class='methods'><li data-type='method' style='display: none;'><a href="Sendmail.html#send">send</a></li><li data-type='method' style='display: none;'><a href="Sendmail.html#template">template</a></li></ul></li></ul>
</nav>

<div id="main">
    
    <h1 class="page-title">sendmail.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>'use strict';

const nodemailer = require('nodemailer');
const handlebars = require('handlebars');
const marked = require('marked');
class Sendmail {
  /**
   * **构造器**
   *
   * `config.nodemailer`：从外部指定`nodemailer`，若未指定则使用内置`nodemailer`。
   *
   * @param {Object} [config={}] 插件配置
   */
  constructor(config = {}) {
    this.config = config;
    this.nodemailer = config.nodemailer || nodemailer;
  }

  /**
   * **发送邮件**
   *
   * @see https://nodemailer.com/message/
   * @param {Object} mail 参数，`nodemailer`的`message`，支持其所有参数。
   *  但不会被编译和转换，一般用于设置接收人。
   * @param {Object} [template={}] [模板]{@link Sendmail#template}
   * @param {Any} [data] 数据
   * @return {Promise} 结果
   * @memberof Sendmail
   */
  send(mail, template = {}, data) {
    const { nodemailer, config } = this;
    const { smtp, from } = config;
    return new Promise((resolve, reject) => {
      if (!mail || !mail.to) {
        const error = new Error();
        error.name = 'EggSendmailError';
        error.code = 400;
        error.message = 'mail receiver required';
        return reject(error);
      }
      const content = this.template(template, data);
      const message = { ...mail, ...content, from };
      const transporter = nodemailer.createTransport(smtp);
      transporter.sendMail(message, (err, res) => {
        if (err) return reject(err);
        return resolve(res);
      });
    });
  }

  /**
   * **处理模板**
   *
   * @see https://nodemailer.com/message/
   * @param {Object} message 消息，`nodemailer`的`message`，支持其所有参数。
   *
   * 其中：
   *
   * - `subject`：支持`Handlebars`模板引擎，数据来源于`data`
   * - `html`：支持`Handlerbars`模板引擎，数据来源于`data`，支持`Markdown`格式编码
   *
   * @param {Any} data 数据
   * @return {Object} 内容
   * @memberof Sendmail
   */
  template(message, data) {
    const subject = handlebars.compile(message.subject)(data);
    const markdown = handlebars.compile(message.html)(data);
    const html = marked(markdown);
    return { ...message, subject, html };
  }
}

module.exports = Sendmail;
</code></pre>
        </article>
    </section>




    
    
</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Wed Mar 11 2020 10:54:30 GMT+0800 (中国标准时间) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>

<script src="scripts/search.js" defer></script>


<script src="scripts/collapse.js" defer></script>


</body>
</html>
